package com.it.springbootmybatis.util;

import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @projectName: springboot-practice
 * @package: com.it.springbootmybatis.util
 * @className: Result
 * @author: Navid
 * @description: TODO
 * @date: 2021/10/23 13:39
 * @version: 1.0
 */
@Data
@NoArgsConstructor
public class Result<T> {

    static final int SUCCESS_CODE = 0;
    static final int ERROR_CODE = -1;

    private boolean success;

    private int code;

    private String msg;

    private T data;

    private Result(int code, T data, String msg) {
        this.success = SUCCESS_CODE == code;
        this.code = code;
        this.data = data;
        this.msg = msg;
    }

    private Result(int resultCode) {
        this(resultCode, null, "成功");
    }

    private Result(int resultCode, String msg) {
        this(resultCode, null, "成功");
    }

    private Result(int resultCode, T data) {
        this(resultCode, data, "成功");
    }

    public static <T> Result<T> success() {
        return new Result<>(SUCCESS_CODE);
    }

    public static <T> Result<T> success(String msg) {
        return new Result<>(SUCCESS_CODE, msg);
    }

    public static <T> Result<T> success(int resultCode, T data, String msg) {
        return new Result<>(resultCode, data, msg);
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(SUCCESS_CODE, data);
    }

    public static <T> Result<T> error(T data) {
        return new Result<>(ERROR_CODE, data, "失败");
    }

    public static <T> Result<T> error() {
        return new Result<>(ERROR_CODE, null, "失败");
    }
    public static <T> Result<T> error(int resultCode, T data) {
        return new Result<>(resultCode, null, "失败");
    }
    public static <T> Result<T> error(int resultCode, T data, String msg) {
        return new Result<>(ERROR_CODE, data, msg);
    }

}
